<template>
    <div class="table-container">
        <div class="data-table">
            <div class="table-header">
                <div class="header-cell header-spacer"></div>
                <div
                    v-for="(header, index) in headers"
                    :key="index"
                    class="header-cell header-fixed">
                    {{ header }}
                </div>
                <div class="header-cell header-spacer"></div>
            </div>

            <div
                v-for="(row, rowIndex) in data"
                :key="rowIndex"
                class="table-row">
                <div class="table-cell table-spacer"></div>
                <div
                    v-for="(cell, cellIndex) in row"
                    :key="cellIndex"
                    class="table-cell table-fixed">
                    <div
                        v-if="cellIndex === 0 && typeof cell === 'object' && cell.icon"
                        class="cell-icon">
                        <img
                            :src="cell.icon"
                            :alt="cell.text" />
                    </div>
                    <span v-if="typeof cell === 'object' && cell.text">{{ cell.text }}</span>
                    <span v-else>{{ cell }}</span>
                </div>
                <div class="table-cell table-spacer"></div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
    interface TableCell {
        text?: string;
        icon?: string;
    }

    interface Props {
        headers: string[];
        data: (string | TableCell)[][];
    }

    const props = defineProps<Props>();
</script>

<style lang="scss" scoped>
    // 导入响应式 mixins
    @use '@/styles/mixins/responsive' as *;

    .table-container {
        width: 100%;
        max-width: 100%;
        margin: 0;
        padding: 0;
        overflow-x: auto;
        position: relative;

        // 自适应滚动条样式
        &::-webkit-scrollbar {
            height: 6px;
        }

        &::-webkit-scrollbar-track {
            background: rgba(0, 0, 0, 0.1);
            border-radius: 3px;
        }

        &::-webkit-scrollbar-thumb {
            background: rgba(0, 0, 0, 0.3);
            border-radius: 3px;

            &:hover {
                background: rgba(0, 0, 0, 0.5);
            }
        }
    }

    .data-table {
        min-width: 100%;
        width: max-content;
    }

    .table-header {
        display: flex;
        background: #f7f7f7;
        font-weight: 700;
        color: #333;
        justify-content: center;
    }

    .table-row {
        display: flex;
        justify-content: center;
        background: #f7f7f7;

        &:nth-child(even) {
            background: #fff;
        }
    }

    .header-cell {
        color: #000000;
        font-family: 'SFProDisplayBold', sans-serif;
        font-weight: 800;
        justify-content: center;
    }

    .header-cell,
    .table-cell {
        padding: pxToRem(16) pxToRem(12);
        display: flex;
        align-items: center;
        font-size: pxToRem(14);
        text-align: center;
        justify-content: center;
        line-height: 1.4;
        word-wrap: break-word;
        min-height: pxToRem(50);

        &:first-child {
            display: flex;
            align-items: center;
            gap: pxToRem(8);
        }
    }

    .header-cell {
        align-items: center;
        justify-content: center;
        text-align: center;
        font-weight: 700;
        min-height: pxToRem(45);
    }

    // 灵活的宽度控制类
    .header-spacer,
    .table-spacer {
        flex: 0.5;
        min-width: pxToRem(20);
        max-width: pxToRem(80);
    }

    .header-fixed,
    .table-fixed {
        flex: 1;
        min-width: pxToRem(200);
        max-width: pxToRem(400);
        width: auto;
    }

    .cell-icon {
        font-size: pxToRem(20);
        display: flex;
        align-items: center;
        margin-right: pxToRem(19);

        img {
            width: pxToRem(20);
            height: pxToRem(20);
        }
    }

    // 响应式设计 - 平板端
    @include tablet {
        .table-container {
            width: 100%;
            margin: 0;
            padding: 0;
        }

        .header-spacer,
        .table-spacer {
            flex: 0.3;
            min-width: pxToRem(15);
            max-width: pxToRem(40);
        }

        .header-fixed,
        .table-fixed {
            flex: 1;
            min-width: pxToRem(120);
            max-width: pxToRem(250);
        }

        .header-cell,
        .table-cell {
            padding: pxToRem(12) pxToRem(10);
            font-size: pxToRem(13);
            line-height: 1.3;
            min-height: pxToRem(45);
            text-align: center;
            align-items: center;
            justify-content: center;
        }

        .header-cell {
            min-height: pxToRem(40);
        }

        .cell-icon {
            margin-right: pxToRem(6);

            img {
                width: pxToRem(18);
                height: pxToRem(18);
            }
        }
    }

    // 响应式设计 - 移动端
    @include mobile {
        .table-container {
            width: 100%;
            margin: 0;
            padding: 0;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }

        .data-table {
            width: 100%;
            min-width: 100%;
        }

        .table-header,
        .table-row {
            flex-wrap: nowrap;
            justify-content: center;
            align-items: stretch;
        }

        .header-spacer,
        .table-spacer {
            flex: 0.1;
            min-width: pxToRem75(8);
            max-width: pxToRem75(15);
        }

        .header-fixed,
        .table-fixed {
            flex: 1;
            min-width: pxToRem75(90);
            max-width: none;
            width: auto;
        }

        .header-cell,
        .table-cell {
            padding: pxToRem75(8) pxToRem75(6);
            font-size: pxToRem75(12);
            white-space: normal;
            text-align: center;
            word-wrap: break-word;
            word-break: break-word;
            line-height: 1.3;
            min-height: pxToRem75(40);
            display: flex;
            align-items: center;
            justify-content: center;
            vertical-align: middle;
        }

        .header-cell {
            font-weight: 700;
            background: rgba(247, 247, 247, 0.95);
            align-items: center;
            justify-content: center;
            text-align: center;
            min-height: pxToRem75(35);
        }

        .table-row {
            &:nth-child(even) {
                background: rgba(255, 255, 255, 0.95);
            }

            &:nth-child(odd) {
                background: rgba(247, 247, 247, 0.95);
            }
        }

        .cell-icon {
            font-size: pxToRem75(12);
            margin-right: pxToRem75(4);
            flex-shrink: 0;

            img {
                width: pxToRem75(16);
                height: pxToRem75(16);
            }
        }
    }
</style>
